# -*- coding: utf-8 -*-
require './perplexity'
require './my_text'
require './vocabulary'
require './alignment'

include Alignment

origin_stdout = $stdout

Perplexity.test
Vocabluary.test
primitive_iteration_test
better_test

fr_dir_name = "../data/en/1988"
en_dir_name = "../data/fr/1988"

en_full_text = []
fr_full_text = []

# number = 0
# Dir.foreach(fr_dir_name) { |file_url|
#   next if ['.', '..'].include? file_url
#   number += 1
#   break if number > 1
#   puts "== parsing file "+File.join(fr_dir_name, file_url)

#   puts "loading fr"
#   fr_file = File.join fr_dir_name, file_url
#   fr_text = MyText.new fr_file

#   puts "loading en"
#   en_file = File.join en_dir_name, file_url.gsub("en", "fr")
#   en_text = MyText.new en_file

#   mm = [en_text.sentences.size, fr_text.sentences.size].min

#   en_full_text += en_text.sentences[0..mm]
#   fr_full_text += fr_text.sentences[0..mm]
# }

fr_text = MyText.new "ru"
en_text = MyText.new "en"

mm = [en_text.sentences.size, fr_text.sentences.size].min

en_full_text += en_text.sentences[0..mm]
fr_full_text += fr_text.sentences[0..mm]

text = en_full_text.zip fr_full_text

# text.delete_if { |pair|
#   pair[0].size>20 or pair[1].size>20 }
# text.delete_if { |pair|
#   pair[0].size<11 or pair[1].size<11 }
text = text[0..100]

# text = [[[:cat, :and, :dog], [:кот, :и, :собака]],
#         [[:cat1, :and, :dog1], [:вода, :и, :воздух]],
#         [[:cat2, :and, :dog2], [:кит, :и, :пень]],
#         [[:cat3, :and, :dog3], [:квадрат, :и, :круг]]]

en = []
fr = []

text.each { |pair|
  puts pair[0].join " "
  en += pair[0]
  puts pair[1].join " "
  fr += pair[1]
  puts ""
}

puts "outputing results"

verbose = 2

v = Vocabluary.new(en.flatten.uniq,
                   fr.flatten.uniq)
(1..7).each { |counter|
  puts "iteration "+counter.to_s
  v = model1_iter(text, v, true)
}


$stdout = File.new "./vocabular.dat", "w"

begin
  v.print_n_best 10
rescue
  puts $!
end

$stdout = origin_stdout

#puts best_alignment(fr_text.sentences[3], en_text.sentences[3], v)
